package com.example.kotlinstudy.类与对象

class Person {
    //一个类可以有一个主构造函数以及一个或多个次构造函数
    ///构造函数
    class Person constructor(firstName: String) {}

    //如果主构造函数没有任何注解或者可见性修饰符，可以省略这个 constructor 关键字
    class Person1(firstName: String)

    //在实例初始化期间，初始化块按照它们出现在类体中的顺序执行，与属性初始化器交织在一起
    class InitOrderDemo(name: String) {
        val firstProperty = "First property: $name".also(::println)

        init {
            println("First initializer block that prints $name")
        }

        val secondProperty = "Second property: ${name.length}".also(::println)

        init {
            println("Second initializer block that prints ${name.length}")
        }

        //主构造的参数可以在初始化块中使用。它们也可以在类体内声明的属性初始化器中使用：
        val customerKey = name.uppercase()
    }

    //声明类型、初始化
    class Person2(val firstName: String, val lastName: String, var age: Int)
    class Person3(val firstName: String, val lastName: String, var isEmployed: Boolean = true)
    //造函数有注解或可见性修饰符，这个 constructor 关键字是必需的，并且这些修饰符在它前面
    //class Customer public @Inject constructor(name: String) { /*……*/ }

    class DontCreateMe private constructor() {}
    class Customer(val customerName: String = "")
}

class Person2 {
    class Person2 constructor(val pets: MutableList<Pet> = mutableListOf())
    class Pet
        (owner: Person2) {
        init {
            owner.pets.add(this)
        }
    }
}

class PersonExample(val name: String) {
    private val children: MutableList<PersonExample> = mutableListOf()
    constructor(name: String, parent: PersonExample) : this(name) {
        parent.children.add(this)
    }
}

class Constructors {
    init {
        println("Init block")
    }
    constructor(i: Int) {
        println("Constructor $i")
    }
}

//抽象类
abstract class Polygon {
    abstract fun draw()
}
class Rectangle: Polygon() {
    override fun draw() {
        TODO("Not yet implemented")
    }
}

//可以用一个抽象成员覆盖一个非抽象的开放成员
open class PolygonExample {
    open fun draw() {

    }
}
abstract class WildShape : PolygonExample() {
    abstract override fun draw()
}


fun main() {
    Person.InitOrderDemo("hello")
    Constructors(1);
    //创建类的实例
//    val invoice = Invoice()
//    val customer = Customer("Joe smith")
    //类成员
}